//
// Created by 董一龙 on 2022/10/13.
//

#include "fqueue/segment_queue/segment_queue.h"
#include <utility>

namespace fqueue {
    std::string segment_queue::index_t::to_string() const {
        return { base_directory + '/' + std::to_string(seqnum) + ".fqueue" };
    }

    std::filesystem::path segment_queue::index_t::to_path() const {
        return { to_string() };
    }

    bool segment_queue::index_t::operator==(const segment_queue::index_t &other) const {
        return to_string() == other.to_string();
    }

    segment_queue::segment_queue(segment_queue::index_t index) : m_index(std::move(index)) {
        m_pQueue = std::make_unique<mmap_queue>(m_index.to_string());
    }

    segment_queue::index_t const& segment_queue::index() {
        return m_index;
    }

    segment_queue::index_t segment_queue::next_index() {
        return { m_index.base_directory , m_index.seqnum + 1 };
    }

    size_t segment_queue::capacity() const {
        assert(m_pQueue != nullptr);
        return m_pQueue->capacity();
    }

    size_t segment_queue::size() const {
        assert(m_pQueue != nullptr);
        return m_pQueue->size();
    }

    int segment_queue::enqueue(void const* input, size_t input_length) {
        assert(m_pQueue != nullptr);
        return m_pQueue->enqueue(input, input_length);
    }

    int segment_queue::dequeue(void *output, size_t output_length) {
        assert(m_pQueue != nullptr);
        return m_pQueue->dequeue(output, output_length);
    }

} // fqueue